package org.example.controller;

import cn.hutool.core.io.IoUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.alipay.api.domain.Person;
import org.apache.ibatis.annotations.Param;
import org.example.config.R;
import org.example.domain.Deposits;
import org.example.domain.DepositsUnit;
import org.example.domain.DepositsVo;
import org.example.domain.Stash;
import org.example.service.DepositUnitService;
import org.example.service.DepositsService;
import org.example.service.StashService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("deposits")
public class DepositsController {
    @Autowired
    DepositsService depositsService;

    @PostMapping("stashByIdAndTitleAndStatus")
    public R stashByIdAndTitleAndStatus(@RequestBody Deposits deposits,Integer id){
        System.err.println(deposits);
        List<Deposits> list = depositsService.stashByIdAndTitleAndStatus(deposits,id);
        return R.successed(list);
    }

    @RequestMapping("depositsList")
    public R depositsList(@RequestBody DepositsVo depositsVo){
        List<Deposits> list = depositsService.depositsList(depositsVo);

        //配置
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
// 自定义属性名 都有默认值的
        treeNodeConfig.setWeightKey("order");
        treeNodeConfig.setIdKey("rid");
// 最大递归深度
        treeNodeConfig.setDeep(3);

//转换器 (含义:找出父节点为字符串零的所有子节点, 并递归查找对应的子节点, 深度最多为 3)
        List<Tree<String>> treeNodes = TreeUtil.<Deposits, String>build(list, "0", treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getId()+"");
                    tree.setParentId(treeNode.getParentId()+"");
                    tree.setName(treeNode.getTitle());
                    // 扩展属性 ...
                    tree.putExtra("id",treeNode.getId());
                    tree.putExtra("title",treeNode.getTitle());
                    tree.putExtra("name",treeNode.getName());
                    tree.putExtra("path", treeNode.getPath());
                    tree.putExtra("tag",treeNode.getTag());
                    tree.putExtra("maxCapacity",treeNode.getMaxCapacity());
                    tree.putExtra("unit",treeNode.getUnit());
                    tree.putExtra("status",treeNode.getStatus());
                    tree.putExtra("context",treeNode.getContext());
                    tree.putExtra("parentAddr",treeNode.getParentAddr());
                    tree.putExtra("version",treeNode.getVersion());
                    tree.putExtra("context",treeNode.getContext());
                    //tree.putExtra("other", new Object());
                });
        System.out.println("treeNodes="+treeNodes);
        return R.successed(treeNodes);
    }
    @Autowired
    StashService stashService;
    @PostMapping("stashList")
    public R stashList(){
        List<Stash> list = stashService.nameList();
        return R.successed(list);
    }


    @RequestMapping("depositsPath")
    public R depositsPath(Integer depositId){
        List<Deposits> list = depositsService.depositsName(depositId);
        //配置
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
// 自定义属性名 都有默认值的
        treeNodeConfig.setWeightKey("order");
        treeNodeConfig.setIdKey("rid");
// 最大递归深度
        treeNodeConfig.setDeep(3);

//转换器 (含义:找出父节点为字符串零的所有子节点, 并递归查找对应的子节点, 深度最多为 3)
        List<Tree<String>> treeNodes = TreeUtil.<Deposits, String>build(list, "0", treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getId()+"");
                    tree.setParentId(treeNode.getParentId()+"");
                    tree.setName(treeNode.getTitle());
                    // 扩展属性 ...
                    tree.putExtra("label",treeNode.getTitle());
                    //tree.putExtra("value", treeNode.getId());
                    tree.putExtra("id",treeNode.getId());
                });
        System.out.println("treeNodes="+treeNodes);
        return R.successed(treeNodes);
    }
    @Autowired
    DepositUnitService depositUnitService;
    @RequestMapping("depositUnit")
    public R depositUnit(){
        List<DepositsUnit> list = depositUnitService.depositUnit();
        return R.successed(list);
    }

    @RequestMapping("depositsAdd")
    public R depositsAdd(@RequestBody Deposits deposits){
        return depositsService.depositsAdd(deposits);
    }

    @RequestMapping("delById")
    public R delById(Integer id){
        return depositsService.delById(id);
    }

    @RequestMapping("updateDeposits")
    public R updateDeposits(@RequestBody Deposits deposits){
        return depositsService.updateDeposits(deposits);
    }

//    @RequestMapping("daoru")
//    public List<Deposits> importExcel(MultipartFile file) throws IOException {
//        ExcelReader reader = ExcelUtil.getReader(file.getInputStream());
//        reader.addHeaderAlias("主键", "id");
//        reader.addHeaderAlias("名称", "title");
//        reader.addHeaderAlias("所属仓库", "depositId");
//        reader.addHeaderAlias("全路径", "path");
//        reader.addHeaderAlias("标签", "tag");
//        reader.addHeaderAlias("最大容量", "maxCapacity");
//        reader.addHeaderAlias("存放内容单位", "stashUnit");
//        reader.addHeaderAlias("状态", "status");
//        reader.addHeaderAlias("上级id", "parentId");
//        reader.addHeaderAlias("上级位置", "parentAddr");
//        reader.addHeaderAlias("备注", "context");
//        List<Deposits> depositsList = reader.readAll(Deposits.class);
//        return depositsList;
//    }
    @Autowired
    HttpServletResponse response;
    @RequestMapping("excel")
    public R excel() throws IOException {
        // 通过工具类创建writer
        ExcelWriter writer = ExcelUtil.getWriter(true);

//自定义标题别名
        writer.addHeaderAlias("title","名称");
        writer.addHeaderAlias("depositId","所属仓库");
        writer.addHeaderAlias("name","所属仓库名称");
        writer.addHeaderAlias("path","全路径" );
        writer.addHeaderAlias("tag","标签" );
        writer.addHeaderAlias("maxCapacity","最大容量" );
        writer.addHeaderAlias("stashUnit","存放内容单位" );
        writer.addHeaderAlias("status","状态" );
        writer.addHeaderAlias("parentId","上级id" );
        writer.addHeaderAlias("parentAddr","上级位置" );
        writer.addHeaderAlias("context","备注" );
        List<Deposits> list = depositsService.list();
        writer.write(list, true);

        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        response.setHeader("Content-Disposition","attachment;filename=test.xlsx");
        ServletOutputStream out=response.getOutputStream();

        writer.flush(out, true);
        writer.close();
        IoUtil.close(out);
        return R.successed(null);
    }

    @RequestMapping("readExcel")
    public void readExcel(){
        ExcelReader reader = ExcelUtil.getReader("D:/qq下载/下载/test (10).xlsx");
        List<List<Object>> readAll = reader.read();

        System.out.println("readAll"+readAll);
    }

}
